package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterCliennt;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-05
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterCliennt ucenterCliennt;




    //根据统计日期生成统计数据
    @Override
    public void createStaDaily(String day) {
        //1.清除相关数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);//统计日期
        baseMapper.delete(wrapper);
        //2.夸模块统计数据，以注册信息为例
        R r = ucenterCliennt.countRegister(day);
        Integer registerNum = (Integer) r.getData().get("countRegister");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO
        //100,200之间
//创建统计对象
        StatisticsDaily daily = new StatisticsDaily();

        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);
    }

//    根据参数查询统计数据
    @Override
    public Map<String, Object> getStaDaily(String type, String begin, String end) {
        //1.拼sql查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staDailieList = baseMapper.selectList(wrapper);//查询的集合数据
        HashMap<String, Object> StaDailyMap = new HashMap<>();//封装结果
        ArrayList<String> dateCalculatedList = new ArrayList<>();//统计日期的集合
        ArrayList<Integer> dataList = new ArrayList<>();//存数据

        for (int i = 0; i < staDailieList.size(); i++) {
            //2.封装x轴数据
            StatisticsDaily daily = staDailieList.get(i);
            dateCalculatedList.add(daily.getDateCalculated());//统计日期的集合getDateCalculated统计日期
            //3封装轴数据
            switch (type) {
                case "register_num": //注册人数
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num"://"登入人数":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num"://每日播放视频数
                    dataList.add(daily.getVideoViewNum());
                    break;
                case  "course_num"://每日新增课程数
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }

        }
        StaDailyMap.put("dateCalculatedList", dateCalculatedList);//统计日期的集合
        StaDailyMap.put("dataList",dataList );//存数据
        return StaDailyMap;
    }


}
